package com.erp.erp_ui.Company;

import com.erp.erp_entitys.Company.CompanyEntity;
import com.erp.erp_entitys.User.UserEntity;
import com.erp.erp_servers.Company.ICompanyService;
import com.erp.erp_servers.OperationLog.IOperationLogService;
import com.erp.erp_ui.base.Base;
import com.erp.utils.R;
import com.erp.utils.ResultStatusEnum;
import com.erp.vo.SelectVo;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;
import java.util.Map;


/**
 * @ClassName Company
 * @Description 公司信息主表
 * @Author songjizhen
 * @Date 2020-06-29 19:47
 * @Version 1.0
 */
@Controller
@RequestMapping(value = "company")
 public class CompanyControl extends Base {

    @Autowired
    ICompanyService companyService;
    @Autowired
    IOperationLogService operationLogService;
    /**
     * 获取公司主信息
     * @param pid
     */
    @RequestMapping(value = "selectByPid")
    @ResponseBody

    public R<List<CompanyEntity>> selectByPid(String pid){

        try {
            List<CompanyEntity> companyEntities = companyService.selectByExample(pid);

            return R.build(companyEntities);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }

     }

    /**
     * 新增一条公司主信息
     * @param
     * @return
     */
    @RequestMapping(value = "insert",method = RequestMethod.POST)
    @ResponseBody

     public R<Integer> insert(@RequestBody Map<String,Object> map){//@RequestBody CompanyEntity company
        try {
           int i = companyService.insertSelective(comconvermap(map));
           if(i>0){
               UserEntity user = (UserEntity) this.getBean("user");
               operationLogService.insertlog("新增",user.getCode(),map.get("pid").toString(),"新增了"+map.get("name"));
           }
            return R.build(i);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
     }
private CompanyEntity comconvermap(Map<String,Object> map){
    /*{"id":data.id,"pid":data.pid,"name":data.name,"englishname":data.englishname
            ,"abbreviation":data.abbreviation,"address":data.address
            ,"englishaddress":data.englishaddress,"postalcode":data.postalcode
            ,"customsno":data.customsno,"taxregistrationno":data.taxregistrationno
            ,"formano":data.formano,"corno":data.corno
            ,"legalperson":data.legalperson,"mailiaddress":data.mailiaddress
            ,"tel":data.tel,"fax":data.fax
            ,"homepage":data.homepage,"newspapersw":data.newspapersw
            ,"natureofcompany":data.natureofcompany,"businessscope":data.businessscope
            ,"chinalogo":data.chinalogo,"enginshlogo":data.enginshlogo
            ,"isproxy":data.isproxy,"policyno":data.policyno*/
    CompanyEntity company=new CompanyEntity();
    company.setAbbreviation(""+map.get("abbreviation"));
    company.setAddress(""+map.get("address"));
    company.setBusinessscope(""+map.get("businessscope"));
    company.setChinalogo(""+map.get("chinalogo"));
    company.setCorno(""+map.get("corno"));
    company.setCustomsno(""+map.get("customsno"));
    company.setEnginshlogo(""+map.get("enginshlogo"));
    company.setEnglishaddress(""+map.get("englishaddress"));
    company.setEnglishname(""+map.get("englishname"));
    company.setFax(""+map.get("fax"));
    company.setFormano(""+map.get("formano"));
    company.setHomepage(""+map.get("homepage"));
    company.setIsproxy(Integer.parseInt(map.get("isproxy").toString()));
    company.setLegalperson(""+map.get("legalperson"));
    company.setMailiaddress(""+map.get("mailiaddress"));
    company.setName(""+map.get("name"));
    company.setNatureofcompany(""+map.get("natureofcompany"));
    company.setNewspapersw(""+map.get("newspapersw"));
    company.setPid(""+map.get("pid"));
    company.setCode(""+map.get("pid"));
    company.setPolicyno(""+map.get("policyno"));
    company.setPostalcode(""+map.get("postalcode"));
    company.setTaxregistrationno(""+map.get("taxregistrationno"));
    company.setTel(""+map.get("tel"));
    //company.setTopDepartment(""+map.get(""));
    //company.setId();

    return company;
}
    /**
     * 更新公司主信息
     * @param
     * @return
     */
    @RequestMapping(value = "update",method = RequestMethod.POST)
    @ResponseBody

    public R<Integer> update(@RequestBody Map<String,Object> map){//CompanyEntity company
        try {
            CompanyEntity comconvermap = comconvermap(map);
            comconvermap.setId(Integer.parseInt(""+map.get("id")));
            int i = companyService.update(comconvermap);
            if(i>0){
                UserEntity user = (UserEntity) this.getBean("user");
                operationLogService.insertlog("更新",user.getCode(),map.get("pid").toString(),"更新了主数据信息"+comconvermap.getId());
            }
            return R.build(i);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
     }

    /**
     * 删除公司主信息
     * @param id
     * @return
     */
    @RequestMapping(value = "delete",method = RequestMethod.POST)
    @ResponseBody

    public R<Integer> delete(int id){
        try {
            int i = companyService.deleteByPrimaryKey(id);
            if(i>0){
                CompanyEntity companyEntity = companyService.selectByPrimaryKey(id);
                UserEntity user = (UserEntity) this.getBean("user");
                operationLogService.insertlog("删除",user.getCode(),companyEntity.getPid(),"删除了"+companyEntity.getName());
            }
            return R.build(i);
        }catch (Exception e){
            e.printStackTrace();
            return R.buildError(ResultStatusEnum.ERROR);
        }
    }

    /**
     * 查询公司name和code
     * @return 制单公司列表
     */
    @RequestMapping(value = "getAllMakerUnit")
    @ResponseBody
    @ApiOperation(value = "查询制单公司name和code", notes = "查询制单公司name和code", httpMethod = "POST")
    public R<List<SelectVo>> getAllMakerUnit(){
        try {
            Object cacheUser = this.getBean("user");
            if (null!=cacheUser){
                List<SelectVo> allList = companyService.getAllList();
                if (allList==null){
                    return  R.buildError(ResultStatusEnum.SELECT_LIST_ISNULL_ERROR);
                }
                return R.build(allList);
            }else{
                return  R.buildError(ResultStatusEnum.TOKEN_FAILURE);
            }
        }catch (Exception e){
            e.printStackTrace();
            return  R.buildError(ResultStatusEnum.ERROR);
        }
    }


}
